package com.example.util;

import java.io.ObjectStreamException;
import java.util.HashMap;
import java.util.Map;

public enum AbstractEnum {
	
	test("","",1);
	public int value;
	public String name;
	public String  className;
	protected static Map<String, Map<String, AbstractEnum>> nameMap = new HashMap<String, Map<String, AbstractEnum>>(16);
	protected static Map<String, Map<Integer, AbstractEnum>> valueMap = new HashMap<String, Map<Integer, AbstractEnum>>(16);
    
	AbstractEnum(String className,String name, int value) {
	    	this.className = className;
	        this.value = value;
	        this.name = name;
	       
	    }
	AbstractEnum(String name, int value) {
    	this.className = this.getClass().getName();
        this.value = value;
        this.name = name;
       
    }
	 protected void add() {
	    	Map<String, AbstractEnum> nameMaps = null;
	    	Map<Integer, AbstractEnum> valueMaps = null;
	    	if (!nameMap.containsKey(this.className)) {
	    		nameMaps = new HashMap<String, AbstractEnum>();
				valueMaps = new HashMap<Integer, AbstractEnum>();
				nameMap.put(this.className, nameMaps);
				valueMap.put(this.className, valueMaps);
	    	} else {
	    		nameMaps = nameMap.get(this.className);
	    		valueMaps = valueMap.get(this.className);
	    	}
	    	
	    	if (valueMaps.containsKey(new Integer(this.value))) {
	    		throw new RuntimeException("存在已重复的值，请检查，value=" + this.value);
	    	}
	    	nameMaps.put(this.name, this);
	    	valueMaps.put(new Integer(this.value), this);
	    }
		public static AbstractEnum get(String className, String name) {
			return nameMap.get(className).get(name);
		}

		public static AbstractEnum get(String className, int value) {
			return valueMap.get(className).get(new Integer(value));
		}

		public static boolean contain(String className, int value) {
			return valueMap.get(className).containsKey(new Integer(value));
		}
		
		public int getValue() {
			return value;
		}

		public String getName() {
			return name;
		}

		protected Object readResolve() throws ObjectStreamException {
			return get(this.className, this.value);
		}

}
